package cs4game;

public class Bullet extends Projectile{
	
	public boolean isPBullet = true;
	
	public Bullet(World w, Vector3D p, float[] v){
		super(p, w);
		velocity = v;
	}
	
	protected void defineTexture() {
		texture = BlockDefs.get4DFromTerrain(15, 15, 0.1);
	}

	public void move(float time){
		pos.x+=velocity[0]*time/1000;
		pos.y+=velocity[1]*time/1000;
		pos.z+=velocity[2]*time/1000;
		
		
		box = new BoundingBox(-1*width/2, width/2, -1*height/2, height/2, -1*depth/2, depth/2);
		box = shiftedBoundingBox();
		
		for(Vector3D p: world.allBlocks.keySet()){
			
			int id = world.allBlocks.get(p);
			BlockID def = BlockDefs.get(getID(id));
			boolean isPIndes = false;
			for(Vector3D tempP : world.indestructibleBs){
				if(p.x == tempP.x && p.y == tempP.y && p.z == tempP.z){
					isPIndes = true;
				}
			}
			
			if(def.getBoundingBox(p, id).intersects(box) && !isPIndes){
				this.destroy();
				world.removeBlock(p);
			}else if(def.getBoundingBox(p, id).intersects(box) && isPIndes){
				this.destroy();
			}
		}//
		for(MobEntity e: world.allEntities){
			box = new BoundingBox(-1*width/2, width/2, -1*height/2, height/2, -1*depth/2, depth/2);
			e.box = new BoundingBox(-1*width/2, width/2, -1*height/2, height/2, -1*depth/2, depth/2);
			//box = shiftedBoundingBox();
			if(this.shiftedBoundingBox().intersects(e.shiftedBoundingBox())){
				if(e instanceof Player && !this.isPBullet){
					world.getPlayer().kill();
					world.removeEntity(this);
				}else if(e instanceof Enemy && this.isPBullet ){
					world.removeEntity(e);
					world.removeEntity(this);
				}
			}
		}
		if(Math.abs(pos.x) > 15 || Math.abs(pos.z) > 15){
			this.destroy();
		}
	}
	
	public void destroy(){
		world.removeEntity(this);
	}
}
